package com.tangsm.spring.boot.deferred.result.service.impl;

import com.tangsm.spring.boot.deferred.result.service.TLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * Tlog 轻量级的分布式日志标记追踪业务接口
 *
 * @author tangsm
 */
@Service
public class TLogServiceImpl implements TLogService {
    private static final Logger log = LoggerFactory.getLogger(TLogServiceImpl.class);

    /**
     * 打招呼
     */
    @Override
    public String sayHello() {
        log.info("你好世界！");
        log.info("你好中国！");
        log.info("你好湖南！");
        log.info("你好永州！");
        return "你好中国";
    }

    /**
     * 异步任务
     *
     * @return 结果
     */
    @Async
    @Override
    public Future<String> doTask() {
        log.info("开始做任务一（睡眠2s）");
        methodThread();
        log.info("任务一完成，当前线程为 {}，请求方法为 {}", Thread.currentThread().getName(), "doTask");
        return new AsyncResult<>("异步任务处理完成");
    }

    /**
     * 接口睡眠
     */
    private void methodThread() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            log.error("sleep异常：{}", e.getMessage(), e);
        }
    }
}
